package com.bestcem.xm.member.dao.impl.mysql;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import com.bestcem.xm.member.dao.SourceCardDao;
import com.bestcem.xm.member.dao.mapper.SourceCardMapper;
import com.bestcem.xm.member.entity.mysql.SourceCardDo;
import com.bestcem.xm.member.entity.mysql.SourceCardEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 来源名片数据访问
 * @date 2021-08-02
 */
@Repository("sourceCardDao")
public class SourceCardDaoImpl implements SourceCardDao {

    @Autowired
    private SourceCardMapper sourceCardMapper;

    /**
     * 统计来源名片
     *
     * @param orgId
     * @param memberId
     * @return
     */
    @Override
    public long count(String orgId, String memberId) {
        return sourceCardMapper.count(orgId, memberId);
    }

    /**
     * 分页获取来源名片列表
     *
     * @param page
     * @param size
     * @param orgId
     * @param memberId
     * @return
     */
    @Override
    public List<SourceCardDo> pageList(Integer page, Integer size, String orgId, String memberId) {
        int pageNum = (null == page || page <= 0) ? 1 : page;
        int pageSize = (null == size || size <= 0) ? 10 : size;
        int offset = (pageNum - 1) * pageSize;
        List<SourceCardEntity> sourceCardEntities =  sourceCardMapper.findPageByMidAndOrgId(memberId, orgId, offset, pageSize);
        return convertEntitysToDos(sourceCardEntities);
    }

    /**
     * 根据联系人标识查询来源名片列表
     * @param id 联系人标识
     * @return java.util.List<com.bestcem.xm.member.entity.mysql.SourceCardDo>
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2021/10/11 13:22
     */
    @Override
    public List<SourceCardDo> findByMid(String id) {
        List<SourceCardEntity> sourceCardEntities = sourceCardMapper.findByMid(id);
        return convertEntitysToDos(sourceCardEntities);
    }

    /**
     * 保存来源名片信息
     *
     * @param card 来源名片信息
     * @return void
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2021/10/11 13:24
     */
    @Override
    public void save(SourceCardDo card) {
        SourceCardEntity sourceCardEntity = convertDoToEntity(card);
        if (null == sourceCardEntity) {
            return;
        }
        // 设置id
        sourceCardEntity.setId(StringUtil.getId());
        // 创建时间
        sourceCardEntity.setCreateTime(DateUtil.getCommonDate());
        // 修改时间
        sourceCardEntity.setUpdateTime(sourceCardEntity.getCreateTime());
        sourceCardMapper.insert(sourceCardEntity);
    }

    @Override
    public void upsert(SourceCardDo card) {
        SourceCardEntity sourceCardEntity = convertDoToEntity(card);
        // SourceCardEntity temp = sourceCardMapper.findByMidAndSource(card.getMemberId(), card.getSource());
        sourceCardEntity.setUpdateTime(DateUtil.getCommonDate());
//        if(temp == null){
//            sourceCardEntity.setCreateTime(sourceCardEntity.getUpdateTime());
//            sourceCardMapper.insert(sourceCardEntity);
//            return;
//        }

        sourceCardMapper.updateByMidAndSource(card.getMemberId(), card.getSource(), sourceCardEntity);
    }

    /**
     * 更新来源名片的data信息
     *
     * @param data   需要更新的来源名片的data信息
     * @param id     联系人标识
     * @param source 来源
     * @param orgId
     * @return void
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2021/10/11 13:44
     */
    @Override
    public void updateDataByMidAndSource(JSONObject data, String id, int source, String orgId) {
        SourceCardDo sourceCardDo = new SourceCardDo();
        sourceCardDo.setId(StringUtil.getId());
        sourceCardDo.setMemberId(id);
        sourceCardDo.setSource(source);
        sourceCardDo.setOrgId(orgId);
        sourceCardDo.setData(data.toJSONString());
        upsert(sourceCardDo);
    }

    /**
     * do 转 entity
     * @param sourceCardDo 来源名片信息do
     * @return com.bestcem.xm.member.entity.mysql.SourceCardEntity
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2021/10/11 13:17
     */
    private SourceCardEntity convertDoToEntity(SourceCardDo sourceCardDo) {
        if (null == sourceCardDo) {
            return null;
        }
        if (StrUtil.isEmpty(sourceCardDo.getId())) {
            sourceCardDo.setId(StringUtil.getId());
        }
        return BeanUtil.toBean(sourceCardDo, SourceCardEntity.class);
    }

    /**
     * entity 转 do
     * @param sourceCardEntity 来源名片entity
     * @return com.bestcem.xm.member.entity.mysql.SourceCardDo
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2021/10/11 13:17
     */
    private SourceCardDo convertEntityToDo(SourceCardEntity sourceCardEntity) {
        if (null == sourceCardEntity) {
            return null;
        }
        return BeanUtil.toBean(sourceCardEntity, SourceCardDo.class);
    }

    /**
     * entitys 转 dos
     * @param sourceCardEntities 来源名片entity列表
     * @return java.util.List<com.bestcem.xm.member.entity.mysql.SourceCardDo>
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2021/10/11 13:20
     */
    private List<SourceCardDo> convertEntitysToDos(List<SourceCardEntity> sourceCardEntities) {
        if (CollectionUtils.isEmpty(sourceCardEntities)) {
            return new ArrayList<>();
        }
        List<SourceCardDo> result = new ArrayList<>();
        for (SourceCardEntity sourceCardEntity : sourceCardEntities) {
            SourceCardDo sourceCardDo = BeanUtil.toBean(sourceCardEntity, SourceCardDo.class);
            result.add(sourceCardDo);
        }
        return result;
    }
}